#coding=utf-8
#Boa:Dialog:DlgRouteEditor

import wx
import sys
from product import CDataDeliveryRoute,CDataDeliveryRouteRegion,CDataCustomerRegion

def create(parent):
    return DlgRouteEditor(parent)

[wxID_DLGROUTEEDITOR, wxID_DLGROUTEEDITORBTNADD, wxID_DLGROUTEEDITORBTNCANCEL, 
 wxID_DLGROUTEEDITORBTNCONFIRM, wxID_DLGROUTEEDITORBTNDEL, 
 wxID_DLGROUTEEDITORBTNDOWN, wxID_DLGROUTEEDITORBTNUP, wxID_DLGROUTEEDITORLC, 
 wxID_DLGROUTEEDITORTCMEMO, wxID_DLGROUTEEDITORTCNAME, 
 wxID_DLGROUTEEDITORTMEMO, wxID_DLGROUTEEDITORTNAME, 
 wxID_DLGROUTEEDITORTREGION, 
] = [wx.NewId() for _init_ctrls in range(13)]

class DlgRouteEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGROUTEEDITOR, name=u'DlgRouteEditor',
              parent=prnt, pos=wx.Point(455, 175), size=wx.Size(603, 473),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u8def\u7ebf\u7f16\u8f91')
        self.SetClientSize(wx.Size(595, 439))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tName = wx.StaticText(id=wxID_DLGROUTEEDITORTNAME,
              label=u'\u8def\u7ebf\u540d\u79f0\uff1a', name=u'tName',
              parent=self, pos=wx.Point(40, 40), size=wx.Size(85, 16), style=0)

        self.tcName = wx.TextCtrl(id=wxID_DLGROUTEEDITORTCNAME, name=u'tcName',
              parent=self, pos=wx.Point(136, 32), size=wx.Size(216, 26),
              style=0, value=u'')

        self.tMemo = wx.StaticText(id=wxID_DLGROUTEEDITORTMEMO,
              label=u'\u5907\u6ce8\u8bf4\u660e\uff1a', name=u'tMemo',
              parent=self, pos=wx.Point(40, 80), size=wx.Size(85, 16), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGROUTEEDITORTCMEMO, name=u'tcMemo',
              parent=self, pos=wx.Point(136, 72), size=wx.Size(400, 26),
              style=0, value=u'')

        self.tRegion = wx.StaticText(id=wxID_DLGROUTEEDITORTREGION,
              label=u'\u884c\u7ecf\u533a\u57df\uff1a', name=u'tRegion',
              parent=self, pos=wx.Point(40, 120), size=wx.Size(85, 16),
              style=0)

        self.lc = wx.ListCtrl(id=wxID_DLGROUTEEDITORLC, name=u'lc', parent=self,
              pos=wx.Point(136, 120), size=wx.Size(200, 224),
              style=wx.LC_SINGLE_SEL | wx.LC_HRULES | wx.LC_REPORT | wx.LC_VRULES)
        self.lc.SetLabel(u'')

        self.btnAdd = wx.Button(id=wxID_DLGROUTEEDITORBTNADD, label=u'+',
              name=u'btnAdd', parent=self, pos=wx.Point(352, 144),
              size=wx.Size(24, 26), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGROUTEEDITORBTNADD)

        self.btnDel = wx.Button(id=wxID_DLGROUTEEDITORBTNDEL, label=u'-',
              name=u'btnDel', parent=self, pos=wx.Point(352, 176),
              size=wx.Size(24, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGROUTEEDITORBTNDEL)

        self.btnUp = wx.Button(id=wxID_DLGROUTEEDITORBTNUP, label=u'\u2227',
              name=u'btnUp', parent=self, pos=wx.Point(352, 256),
              size=wx.Size(24, 26), style=0)
        self.btnUp.Bind(wx.EVT_BUTTON, self.OnBtnUpButton,
              id=wxID_DLGROUTEEDITORBTNUP)

        self.btnDown = wx.Button(id=wxID_DLGROUTEEDITORBTNDOWN, label=u'\u2228',
              name=u'btnDown', parent=self, pos=wx.Point(352, 288),
              size=wx.Size(24, 26), style=0)
        self.btnDown.Bind(wx.EVT_BUTTON, self.OnBtnDownButton,
              id=wxID_DLGROUTEEDITORBTNDOWN)

        self.btnConfirm = wx.Button(id=wxID_DLGROUTEEDITORBTNCONFIRM,
              label=u'\u786e\u5b9a', name=u'btnConfirm', parent=self,
              pos=wx.Point(136, 384), size=wx.Size(93, 26), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton,
              id=wxID_DLGROUTEEDITORBTNCONFIRM)

        self.btnCancel = wx.Button(id=wxID_DLGROUTEEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(312, 384), size=wx.Size(93, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGROUTEEDITORBTNCANCEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.lc.InsertColumn(0, u'行经区域', width=150)
    
    def OnBtnConfirmButton(self, event):
        name = self.tcName.GetValue()
        if not name:
            dlg = wx.MessageDialog(self, u'必须设置名称', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        # 保存路线
        self.m_route = CDataDeliveryRoute.factory(True)
        data = {CDataDeliveryRoute.fi_name:self.tcName.GetValue(),
                CDataDeliveryRoute.fi_memo:self.tcMemo.GetValue()}
        
        self.m_route.setMulFieldVal(data)
        self.m_route.insert()
        
        # 保存行经区域及顺序
        for row in range(self.lc.GetItemCount()):
            regionName = self.lc.GetItemText(row)
            regionObj = CDataCustomerRegion.getFirstInstance({CDataCustomerRegion.fi_name:regionName})
            
            routeRegion = CDataDeliveryRouteRegion.factory()
            routeRegion.setMulFieldVal({routeRegion.fi_routeId:self.m_route.getKeyVal(),
                                    routeRegion.fi_regionId:regionObj.getKeyVal(),
                                    routeRegion.fi_index:row})
            routeRegion.insert()
        
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
        
    def OnBtnAddButton(self, event):
        import DlgCustRegion
        
        dlg = DlgCustRegion.create(self)
        if dlg.ShowModal() != wx.ID_OK:
            return
        
        regionObj = dlg.getSelectObj()
        self.lc.InsertStringItem(sys.maxint, regionObj.getValue(regionObj.fi_name))

    def OnBtnDelButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        self.lc.DeleteItem(item)        

    def swap(self, lc, item1, item2):
        for col in range(lc.GetColumnCount()):
            temp = lc.GetItem(item1, col).GetText()
            lc.SetStringItem(item1, col, lc.GetItem(item2, col).GetText())
            lc.SetStringItem(item2, col, temp)
            
    def OnBtnUpButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1 or item == 0):
            return
        
        self.swap(self.lc, item, item-1)
        self.lc.Select(item-1)
        self.lc.SetFocus()

    def OnBtnDownButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1 or item == self.lc.GetItemCount()-1):
            return
        
        self.swap(self.lc, item, item+1)
        self.lc.Select(item+1)
        self.lc.SetFocus()
        
        
